package net.oschina.app.ui.activity;


import android.content.Intent;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.protocol.AccessProtocol;
import net.oschina.app.protocol.Constant;
import net.oschina.app.util.ToolBarUtil;

/**
 * Created by Shinelon on 2017/1/16.
 */

public abstract class WebActivity extends BaseActivity {
    private WebView mWebView;
    private WebSettings mSettings;
    private Object mBody;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView() {
        ToolBarUtil.setToolBar(this,getToolBarTitle());
        ToolBarUtil.setRefreshView(true);
        mWebView = (WebView) findViewById(R.id.wv);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
//        mSettings = mWebView.getSettings();
//        // 启动运行javascript
//        mSettings.setJavaScriptEnabled(true);
    }

    protected abstract String getToolBarTitle();

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String[] data = intent.getStringArrayExtra(Constant.WV);
        if (data!=null&&data.length!=0){
            int id = Integer.parseInt(data[0]);
            final AccessProtocol protocol = getAccessProtocol();
            protocol.setPath(id+".xml");
            protocol.getData();
            protocol.setOnAccessNetListener(new AccessProtocol.onAccessNetListener() {
                @Override
                public void onSuccess() {
                    mBody = protocol.getBody();
                    if (mBody!=null) {
                        mWebView.loadUrl(getUrl(mBody));
                    }
                }

                @Override
                public void onFail() {

                }
            });
        }

    }

    protected abstract String getUrl(Object body);

    protected abstract AccessProtocol getAccessProtocol();
}
